using UnityEngine;
using System.Collections;


[RequireComponent(typeof(HandPointControl))]
public class PushDetector : MonoBehaviour {

    // ----- configurable via Unity -------------------------------------------
    public float size             = 750.0f;
    public float initialValue     = 0.2f;


    // ----- internal variables -----------------------------------------------
    bool isPushed;
    Fader pushFader;


    // ----- internal methods --------------------------------------------------
    void Start() {
        pushFader = gameObject.AddComponent<Fader>();
        pushFader.direction = -Vector3.forward;
    }

    void Hand_Create(Vector3 pos) {
        pushFader.size         = size;
        pushFader.initialValue = initialValue;
        pushFader.MoveTo(pos, initialValue);
        Hand_Update(pos);
    }

    void Hand_Update(Vector3 pos) {
        // move slider if hand is out of its bounds (that way it always feels responsive)
        pushFader.MoveToContain(pos);
        pushFader.Hand_Update(pos);

        // quick out if in cooldown
        if (OpenNISessionManager.Instance.CoolingDown) return;

        if (pushFader.value < 0.5) {
            SendMessage("PushDetector_Release", SendMessageOptions.DontRequireReceiver);
            isPushed = false;
        }
        else if (!isPushed) {
            SendMessage("PushDetector_Push", SendMessageOptions.DontRequireReceiver);
            isPushed = true;
        }
    }

    void Hand_Destroy() {
        if (isPushed) {
            SendMessage("PushDetector_Release", SendMessageOptions.DontRequireReceiver);
            isPushed = false;
        }
    }
}
